package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PointsRule;
import com.ruoyi.system.domain.vo.PointsRankVo;
import com.ruoyi.system.service.IPointsRuleService;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PointsManagement;
import com.ruoyi.system.service.IPointsManagementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分管理Controller
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@RestController
@RequestMapping("/system/points/management")
public class PointsManagementController extends BaseController
{
    @Autowired
    private IPointsManagementService pointsManagementService;
    @Autowired
    private IPointsRuleService pointsRuleService;

    /**
     * 查询积分管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PointsManagement pointsManagement)
    {
        setStartPage(pointsManagement.getPageNum(),pointsManagement.getPageSize());
        List<PointsManagement> list = pointsManagementService.selectPointsManagementList(pointsManagement);
        return getDataTable(list);
    }

    /**
     * 获取积分管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:management:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pointsManagementService.selectPointsManagementById(id));
    }

    /**
     * 新增积分管理
     */
    @PreAuthorize("@ss.hasPermi('system:management:add')")
    @Log(title = "积分管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PointsManagement pointsManagement)
    {
        pointsManagement.setCreateBy(String.valueOf(getUserId()));
        pointsManagement.setCreateName(getUsername());
        return toAjax(pointsManagementService.insertPointsManagement(pointsManagement));
    }

    /**
     * 修改积分管理
     */
    @PreAuthorize("@ss.hasPermi('system:management:edit')")
    @Log(title = "积分管理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody PointsManagement pointsManagement)
    {
        return toAjax(pointsManagementService.updatePointsManagement(pointsManagement));
    }

    /**
     * 删除积分管理
     */
    @PreAuthorize("@ss.hasPermi('system:management:remove')")
    @Log(title = "积分管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(pointsManagementService.deletePointsManagementById(id));
    }

    /**
     * 审核
     */
    @PreAuthorize("@ss.hasPermi('system:management:edit')")
    @Log(title = "积分管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody PointsManagement pointsManagement)
    {
        return toAjax(pointsManagementService.updatePointsManagementStatus(pointsManagement));
    }

    /**
     * 按照年度查询个人积分统计
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @PostMapping("/selectRankByYear")
    public TableDataInfo selectRankByYear(@RequestBody PointsManagement pointsManagement)
    {
        String queryYear = pointsManagement.getQueryYear();
        String effectiveTerm = queryYear;
        if(StringUtils.isBlank(queryYear)){
            String nowYear = DateUtils.getNowYear();
            pointsManagement.setQueryYear(nowYear);
            pointsManagement.setStartTime(nowYear+"-01-01 00:00:00");
            pointsManagement.setEndTime(nowYear+"-12-31 23:59:59");
        }else{
            pointsManagement.setStartTime(queryYear+"-01-01 00:00:00");
            pointsManagement.setEndTime(queryYear+"-12-31 23:59:59");
        }
        PointsRule pointsRule = new PointsRule();
        pointsRule.setEffectiveTerm(effectiveTerm);
        PointsRule pointsRule1 = pointsRuleService.selectPointsRuleByPointsRule(pointsRule);
        pointsManagement.setPointsRuleId(pointsRule1.getId());
        return pointsManagementService.selectRank(pointsManagement);
    }

    /**
     * 按照月度查询个人积分统计
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @PostMapping("/selectRankByMonth")
    public TableDataInfo selectRankByMonth(@RequestBody PointsManagement pointsManagement)
    {
        String queryMonth = pointsManagement.getQueryMonth();
        String effectiveTerm = queryMonth.substring(0,4);
        if(StringUtils.isBlank(queryMonth)){
            String monthFirtDay = DateUtils.getMonthFirtDay();
            String monthLastDay = DateUtils.getMonthLastDay();
            pointsManagement.setStartTime(monthFirtDay+" 00:00:00");
            pointsManagement.setEndTime(monthLastDay+" 23:59:59");
        }else{
            String firstDayOfMonth = DateUtils.getFirstDayOfMonth(queryMonth);
            String lastDayOfMonth = DateUtils.getLastDayOfMonth(queryMonth);
            pointsManagement.setStartTime(firstDayOfMonth+" 00:00:00");
            pointsManagement.setEndTime(lastDayOfMonth+" 23:59:59");
        }
        PointsRule pointsRule = new PointsRule();
        pointsRule.setEffectiveTerm(effectiveTerm);
        PointsRule pointsRule1 = pointsRuleService.selectPointsRuleByPointsRule(pointsRule);
        pointsManagement.setPointsRuleId(pointsRule1.getId());
        return pointsManagementService.selectRank(pointsManagement);
    }


}
